﻿// 结构性设计模式：装饰器设计模式
/*
 
装饰器模式：动态的给一个对象添加一些额外的职责，就增加功能来说，装饰模式比生成子类更为灵活。

 需要给茶添加配料，目前配料有珍珠，布丁，仙草，将来还可能添加更多的配料；

需求：想要一杯奶茶，添加两份布丁，一份珍珠，该如何实现？
 */
Console.WriteLine("Hello, World!");

MilkTea milkTea = new MilkTea();
BuDing buDing1 = new BuDing();
BuDing buDing2 =new BuDing();

Zhenzhu zhenzhu = new Zhenzhu();
buDing1.SetComponent(milkTea);
buDing2.SetComponent(buDing1);
zhenzhu.SetComponent(buDing2);
Console.WriteLine(zhenzhu.Cast());
public abstract class YinLiao
{
    public abstract double Cast();
}

/*==================茶的种类=====================*/
public class MilkTea : YinLiao
{
    public override double Cast()
    {
        Console.WriteLine("奶茶10块钱一杯");
        return 10;
    }
}

public class FruitTea : YinLiao
{
    public override double Cast()
    {
        Console.WriteLine("水果茶15块钱一杯");
        return 15;
    }
}
public class SodaTea : YinLiao
{
    public override double Cast()
    {
        Console.WriteLine("苏打水4块钱一杯!");
        return 4;
    }
}

/// <summary>
/// 装饰器模式第一个核心
/// </summary>
public abstract class Decorator : YinLiao
{
    /// <summary>
    /// 添加一个父类的引用
    /// </summary>
    private YinLiao yinLiao;
    /// <summary>
    /// 通过set方法赋值
    /// </summary>
    /// <param name="yinLiao"></param>
    public void SetComponent(YinLiao yinLiao)
    {
        this.yinLiao = yinLiao;
    }
    public override double Cast()
    {
        return this.yinLiao.Cast();
    }   
}

/*====================配料类======================================*/
public class BuDing : Decorator
{
    private static double money = 5;
    public override double Cast()
    {
        Console.WriteLine("布丁5块");
        //第二个核心
        return base.Cast() + money;
    }
}


public class Zhenzhu : Decorator
{
    private static double money = 7;
    public override double Cast()
    {
        Console.WriteLine("珍珠7块");
        //第二个核心
        return base.Cast() + money;
    }
}
public class XianCao : Decorator
{
    private static double money = 6;
    public override double Cast()
    {
        Console.WriteLine("仙草6块");
        //第二个核心
        return base.Cast() + money;
    }
}